package com.example.mediamoudle.systime;

import android.opengl.GLES20;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView;
import android.view.Surface;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyRenderer implements GLSurfaceView.Renderer {
    private Surface sourceSurface;
    private int sourceTextureId;
    private int fboId;

    public MyRenderer(Surface sourceSurface) {
        this.sourceSurface = sourceSurface;
    }
    // 顶点坐标接收者
    private int mVertexPosHandler = -1 ;
    // 纹理坐标接收者
    private int mTexturePosHandler = -1;
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        // 初始化OpenGL状态
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        // 创建源纹理对象
        int[] textureIds = new int[1];
        GLES20.glGenTextures(1, textureIds, 0);
        sourceTextureId = textureIds[0];
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, sourceTextureId);

        // 配置纹理参数
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

        // 创建帧缓冲对象
        int[] fboIds = new int[1];
        GLES20.glGenFramebuffers(1, fboIds, 0);
        fboId = fboIds[0];
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);

        // 将源纹理附加到帧缓冲的颜色缓冲区
        GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
                GLES20.GL_TEXTURE_2D, sourceTextureId, 0);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // 设置视口尺寸
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        // 清空画布
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

        // 绘制源Surface到GLSurfaceView上
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, sourceTextureId);

        // 设置顶点坐标和纹理坐标，绘制一个矩形
        float[] vertices = {
                -1.0f, 1.0f, 0.0f,
                -1.0f, -1.0f, 0.0f,
                1.0f, -1.0f, 0.0f,
                1.0f, 1.0f, 0.0f
        };

        float[] textureCoords = {
                0.0f, 1.0f,
                0.0f, 0.0f,
                1.0f, 0.0f,
                1.0f, 1.0f
        };

        /*// 设置顶点坐标属性
        int vertexPositionHandle =  -1*//* 程序中获取顶点坐标属性的方法 *//*;
        GLES20.glEnableVertexAttribArray(vertexPositionHandle);
        GLES20.glVertexAttribPointer(vertexPositionHandle, 3, GLES20.GL_FLOAT, false, 0, vertexBuffer);

        // 设置纹理坐标属性
        int textureCoordsHandle = -1*//* 程序中获取纹理坐标属性的方法 *//*;
        GLES20.glEnableVertexAttribArray(textureCoordsHandle);
        GLES20.glVertexAttribPointer(textureCoordsHandle, 2, GLES20.GL_FLOAT, false, 0, textureBuffer);

        // 绘制矩形
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, 4);

        // 解绑属性和纹理
        GLES20.glDisableVertexAttribArray(vertexPositionHandle);
        GLES20.glDisableVertexAttribArray(textureCoordsHandle);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);*/
    }
}

